package core 
{
	import qhInterface.ICaller;
	import flash.utils.*;
	import utils.ArrayUtil;
	
	/**
	 * ...
	 * @author gcxk721
	 */
	public class Caller implements ICaller 
	{
		
		protected var _callerMap:Dictionary;

        public function Caller()
        {
            this._callerMap = new Dictionary(true);
        }

        public function addCall(key : Object, func:Function) : void
        {
            var array : Array = this.getlistByType(key);
            array.push(func);
        }

        public function removeCall(key : Object, func : Function) : void
        {
            var array : Array = this._callerMap[key] as Array;
            if (array && array.length > 0)
            {
                ArrayUtil.removeItem(array, func);
            }
        }

        public function call(key:Object, ... args) : Boolean
        {
            var func : Function = null;
            var array : Array = _callerMap[key];
            if (array && array.length > 0)
            {
                for each (func in array)
                {
                    
                    func.apply(null, args);
                }
            }
            return true;
        }

        public function dispose(value : Boolean = true) : void
        {
            var array : Array = null;
            var key : String = null;
            for (key in this._callerMap)
            {
                
                array = this._callerMap[key] as Array;
                array.length = 0;
                delete this._callerMap[key];
            }
        }

        private function getlistByType(key : Object) : Array
        {
            if (key in this._callerMap)
            {
                return this._callerMap[key];
            }
            var array : Array = new Array();
            this._callerMap[key] = array;
            return array;
        }

        public function removeCallByType(key : Object) : void
        {
            var array : Array = this._callerMap[key] as Array;
            if (array)
            {
                array.length = 0;
                delete this._callerMap[key];
            }
        }

        public function hasCall(key : Object) : Boolean
        {
            return key in this._callerMap;
        }

		
	}

}